package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.CareOrderDao;
import com.aaa.doctor.dao.CareOrderItemDao;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.CareOrderService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.doctor.vo.CareOrderVo;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CareOrderServiceImpl implements CareOrderService {

    @Autowired
    private CareOrderDao careOrderDao;

    @Autowired
    private CareOrderItemDao careOrderItemDao;

    /**
     * 根据病历ID查询处方信息
     * @param chId
     * @return
     */
    @Override
    public Result<List<CareOrder>> queryCareOrdersByChId(String chId) {
        //查询药品处方信息
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(chId)){
            wrapper.eq("ch_id",chId);
        }
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper);
        //遍历处方信息
        for (CareOrder careOrder : careOrders) {
            QueryWrapper<CareOrderItem> wrapper1 = new QueryWrapper<>();
            if (Objects.nonNull(careOrder.getCoId())){
                //查询检查处方信息
                wrapper1.eq("co_id",careOrder.getCoId());
                List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper1);
                careOrder.setCareOrderItemList(careOrderItems);
            }
        }
        return new Result<>(200,"查询处方信息成功",careOrders);
    }

    @Autowired
    private UserFeign userFeign;
    /**
     * 保存用药处方和检查处方
     * @param careOrderVo
     * @return
     */
    @Override
    public Result<String> saveCareOrderItem(CareOrderVo careOrderVo) {

        // 获取药
        CareOrder careOrder = careOrderVo.getCareOrder();
        // 检查
        List<CareOrderItem> careOrderItems = careOrderVo.getCareOrderItems();
        // 获取token
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) TokenUtils.getInfo(request.getHeader("token")).get("username");
        // 用户id
        Long userId = userFeign.getByUsername(phone).getUserId();
        // 用户名
        String userName = userFeign.getByUsername(phone).getUserName();
        //雪花算法
        IdWorker worker = new IdWorker();
        long l = worker.nextId();
        //生成coId 存到his_care_order表中
        String coId ="CO"+ l;
        careOrder.setCoId(coId);
        careOrder.setUserId(userId);
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        careOrder.setCreateTime(now);
        careOrder.setCreateBy(userName);
        careOrderDao.insert(careOrder);
        for (CareOrderItem careOrderItem:careOrderItems){
            //雪花算法
            IdWorker worker2 = new IdWorker();
            long l2 = worker2.nextId();
            //生成coId 存到his_care_order_item表中
            String ItemId ="ITEM"+ l2;
            careOrderItem.setItemId(ItemId);
            careOrderItem.setCoId(coId);
            //获取当前时间
            careOrderItem.setStatus("0");
            LocalDateTime dateTime = LocalDateTime.now();
            careOrderItem.setCreateTime(dateTime);
            careOrderItemDao.insert(careOrderItem);
        }
        return new Result<>(200,"保存用药处方和检查处方");
    }
}
